package com.ms.game.characterInfo;

import java.util.Date;
import java.util.HashMap;

import com.ms.game.characterInfo.db.GetDailyExchangeAsync;
import com.ms.game.characterInfo.db.LoadCharacterInfoAsync;
import com.ms.game.characterInfo.db.SearchCharacterInfoByDateAsync;
import com.ms.game.characterInfo.message.CharacterInfoRequest;
import com.ms.game.characterInfo.message.CharacterInfoResponse;
import com.ms.game.characterInfo.message.DailyExchangeRequest;
import com.ms.game.characterInfo.message.DailyExchangeResponse;
import com.ms.game.common.container.BasicSetting;
import com.ms.game.common.container.GameLimitInfo;
import com.ms.game.common.container.GameTypeInfo;
import com.ms.game.invitefriend.async.GetCommissionDetailAsync;
import com.ms.game.invitefriend.async.GetDownLineListAsync;
import com.ms.game.invitefriend.message.CGetDownLineListRequest;
import com.ms.player.Player;
import com.ms.player.PlayerNickNameParser;
import com.ms.service.error.ErrorCode;
import com.ms.service.module.zone.BlockType;
import com.ms.service.module.zone.module.BasicZoneModule;
import com.xpec.c4.game.datamanager.agent.money.MoneyChangeWay;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.BasicServiceModule;
import com.xpec.c4.service.module.common.PlayerCommonInfoManager;
import com.xpec.c4.service.module.datacenter.player.db.DailyExchangeLogModel;
import com.xpec.c4.service.module.datacenter.player.db.PlayerMoneyChangeLogModel;

public class CharacterInfoProcessor {

	/**
	 * Key - 遊戲館Type
	 * value - logClassName(查詢用table名稱)
	 */
	private static HashMap<String , String> gameTypeToLogClassName;
	
	/**
	 * 從數據檔中取得遊戲館type對logClassName的map，實體只創建一次
	 */
	public static HashMap<String , String> getGameTypeToLogClassName() {
		if(gameTypeToLogClassName == null){
			gameTypeToLogClassName = new HashMap<String , String>();
			for(GameTypeInfo info : BasicSetting.getInstance().getGameTypeInfoList()){
				if(info.getGameType() != null && info.getLogClassName() != null){
					gameTypeToLogClassName.put(info.getGameType(), info.getLogClassName());
				}
			}
		}
		return gameTypeToLogClassName;
	}
	
	/**
	 * @param sessionID
	 * 獲取玩家資訊
	 */
	public static void OnGetCharacterInfo(GUID sessionID , BasicZoneModule module){
		
		CharacterInfoResponse response =new CharacterInfoResponse();
		
		GUID playerID = PlayerCommonInfoManager.getInstance().getPlayerGuidBySession(sessionID);
		if(playerID == null){
			//是否有玩家資料
			response.errorCode = ErrorCode.NO_PLAYER_DATA;
			MessageAction.sendMessage(sessionID, MessageTypes.S_GET_CHARACTER_INFO_RESPONSE, response);
			return;
		}
		
		Player playerData = module.getZone().getPlayer(playerID);
		if(playerData == null){
			//是否有玩家資料
			response.errorCode = ErrorCode.NO_PLAYER_DATA;
			MessageAction.sendMessage(sessionID, MessageTypes.S_GET_CHARACTER_INFO_RESPONSE, response);
			return;
		}
		//有資料
		response.facePhoto = playerData.getFacephoto();
		response.level = playerData.getLevel();
		response.name = PlayerNickNameParser.mergeNickName(playerData.getName(), playerData.getNameNumber());
		response.goldCoin = playerData.getGoldCoin().toString();
		response.luckyCoin = playerData.getLuckyCoin().toString();
		
		//讀取今日押注今日得分和帳號點數
		GUID accountID = PlayerCommonInfoManager.getInstance().getAccountIdByPlayerGuid(playerID);
		LoadCharacterInfoAsync loadData = new LoadCharacterInfoAsync();
		loadData.accountID = accountID;
		loadData.playerID = playerID;
		loadData.response = response;
		loadData.sessionID = sessionID;
		module.addAsyncCommand(loadData);
		
	}
	
	/**
	 * @param response
	 * @param sessionID
	 * 獲取玩家資料後的call back
	 */
	public static void OnGetCharacterInfoCallBack(CharacterInfoResponse response , GUID sessionID){
		
		response.errorCode = ErrorCode.SUCCESS;
		MessageAction.sendMessage(sessionID, MessageTypes.S_GET_CHARACTER_INFO_RESPONSE, response);
	}
	
	/**
	 * @param sessionID
	 * @param date
	 * 透過日期查詢遊戲記錄
	 */
	public static void OnSearchCharacterInfoByDate(GUID sessionID , BasicZoneModule module , CharacterInfoRequest request){
		
		CharacterInfoResponse response =new CharacterInfoResponse();
		
		GUID playerID = PlayerCommonInfoManager.getInstance().getPlayerGuidBySession(sessionID);
		if(playerID == null){
			//是否有玩家資料
			response.errorCode = ErrorCode.NO_PLAYER_DATA;
			MessageAction.sendMessage(sessionID, MessageTypes.S_SEARCH_CHARACTER_GAME_INFO_BY_DATE, response);
			return;
		}
		
		if (module.getZone().isBlock(playerID, BlockType.SUBKEY_SEARCH_CHARACTER_INFO_BY_DATE)) {
			// 忙碌中
			response.errorCode = ErrorCode.GAME_SERVER_BUSY;
			MessageAction.sendMessage(sessionID, MessageTypes.S_SEARCH_CHARACTER_GAME_INFO_BY_DATE, response);
			return;
		}
		
		Player playerData = module.getZone().getPlayer(playerID);
		if(playerData == null){
			//是否有玩家資料
			response.errorCode = ErrorCode.NO_PLAYER_DATA;
			MessageAction.sendMessage(sessionID, MessageTypes.S_SEARCH_CHARACTER_GAME_INFO_BY_DATE, response);
			return;
		}
		
		//確認會員由細記錄限制
		int limitCode = GameLimitInfo.checkLimitByClassify(GameLimitInfo.BASIC_GAME_RECORD, playerData.getVip(),playerData.getLevel(), playerData.isVerifiedMember(), playerData.isTrialAccount(), playerData.getUplineCode());
		if(limitCode != GameLimitInfo.TRUE){
			response.errorCode = GameLimitInfo.limitToErrorCode(limitCode);
			MessageAction.sendMessage(sessionID, MessageTypes.S_SEARCH_CHARACTER_GAME_INFO_BY_DATE, response);
			return;
		}
		
		if(request.date == null || request.date == 0){
			//沒有時間
			response.errorCode = ErrorCode.CHARACTE_NO_DATE_TIME;
			MessageAction.sendMessage(sessionID, MessageTypes.S_SEARCH_CHARACTER_GAME_INFO_BY_DATE, response);
			return;
		}
		
		if((new Date().getTime()-request.date)/86400000 > 30){
			//最多只能給三十天之內的資料
			response.errorCode = ErrorCode.CHARACTE_NO_DATE_TIME;
			MessageAction.sendMessage(sessionID, MessageTypes.S_SEARCH_CHARACTER_GAME_INFO_BY_DATE, response);
			return;
		}
		
		if(request.gameType == null){
			//沒有遊戲館資料
			response.errorCode = ErrorCode.CHARACTE_NO_GAME_TYPE_INFO;
			MessageAction.sendMessage(sessionID, MessageTypes.S_SEARCH_CHARACTER_GAME_INFO_BY_DATE, response);
			return;
		}
		
		String logClassName = getGameTypeToLogClassName().get(request.gameType);
		if(logClassName == null){
			//沒有遊戲館資料
			response.errorCode = ErrorCode.CHARACTE_NO_GAME_TYPE_INFO;
			MessageAction.sendMessage(sessionID, MessageTypes.S_SEARCH_CHARACTER_GAME_INFO_BY_DATE, response);
			return;
		}
		
		SearchCharacterInfoByDateAsync searchData = new SearchCharacterInfoByDateAsync();
		searchData.playerID = playerID;
		searchData.logClassName = logClassName;
		searchData.date = request.date;
		searchData.response = response;
		searchData.sessionID = sessionID;
		module.addAsyncCommand(searchData);
		
		module.getZone().putBlockList(playerID, BlockType.SUBKEY_SEARCH_CHARACTER_INFO_BY_DATE,
				BlockType.SEARCH_CHARACTER_INFO_BY_DATE);
	}

	/**
	 * 取得日常收支清單
	 */
	public static void OnGetDailyExchange(GUID sessionID , BasicZoneModule module , DailyExchangeRequest request) {
		DailyExchangeResponse response = new DailyExchangeResponse();

		GUID playerID = PlayerCommonInfoManager.getInstance().getPlayerGuidBySession(sessionID);
		if(playerID == null){
			//是否有玩家資料
			response.errorCode = ErrorCode.NO_PLAYER_DATA;
			MessageAction.sendMessage(sessionID, MessageTypes.S_GET_DAILY_EXCHANGE_RESPONSE, response);
			return;
		}
		
		if (module.getZone().isBlock(playerID, BlockType.SUBKEY_GET_DAILY_EXCHANGE)) {
			// 忙碌中
			response.errorCode = ErrorCode.GAME_SERVER_BUSY;
			MessageAction.sendMessage(sessionID, MessageTypes.S_GET_DAILY_EXCHANGE_RESPONSE, response);
			return;
		}
		
		Player playerData = module.getZone().getPlayer(playerID);
		if(playerData == null){
			//是否有玩家資料
			response.errorCode = ErrorCode.NO_PLAYER_DATA;
			MessageAction.sendMessage(sessionID, MessageTypes.S_GET_DAILY_EXCHANGE_RESPONSE, response);
			return;
		}
		
		//確認會員收支記錄限制
//		int limitCode = GameLimitInfo.checkLimitByClassify(GameLimitInfo.BASIC_GAME_RECORD, playerData.getVip(),playerData.getLevel(), playerData.isVerifiedMember(), playerData.isTrialAccount(), playerData.getUplineCode());
//		if(limitCode != GameLimitInfo.TRUE){
//			response.errorCode = GameLimitInfo.limitToErrorCode(limitCode);
//			MessageAction.sendMessage(sessionID, MessageTypes.S_GET_DAILY_EXCHANGE_RESPONSE, response);
//			return;
//		}
		
		if(request.date == null || request.date == 0){
			//沒有時間
			response.errorCode = ErrorCode.CHARACTE_NO_DATE_TIME;
			MessageAction.sendMessage(sessionID, MessageTypes.S_GET_DAILY_EXCHANGE_RESPONSE, response);
			return;
		}
		
		if((new Date().getTime()-request.date)/86400000 > 30){
			//最多只能給三十天之內的資料
			response.errorCode = ErrorCode.CHARACTE_NO_DATE_TIME;
			MessageAction.sendMessage(sessionID, MessageTypes.S_GET_DAILY_EXCHANGE_RESPONSE, response);
			return;
		}
		
		GetDailyExchangeAsync searchDailyData = new GetDailyExchangeAsync();
		searchDailyData.playerID = playerID;
		searchDailyData.date = request.date;
		searchDailyData.response = response;
		searchDailyData.sessionID = sessionID;
		module.addAsyncCommand(searchDailyData);
		
		module.getZone().putBlockList(playerID, BlockType.SUBKEY_GET_DAILY_EXCHANGE,
				BlockType.GET_DAILY_EXCHANGE);
	}

	/**
	 * 收到取得下線清單的要求
	 * 
	 * @param player
	 *            player data
	 * @param module
	 *            current module
	 * @param request
	 *            request parameter
	 */
	public static void onRecvGetDownLineListRequest(Player player, BasicServiceModule module,
			CGetDownLineListRequest request) {
		if (player == null) {
			return;
		}

		if (module == null) {
			return;
		}

		if (request == null) {
			return;
		}
		
		GetDownLineListAsync task = new GetDownLineListAsync(player, player.getSessionID(), request);
		module.addAsyncCommand(task);
	}

	/**
	 * 收到取得角色的各等級區間抽佣金額加總清單的要求
	 * 
	 * @param player
	 *            player data
	 * @param module
	 *            current module
	 * @param queryPlayerGuid
	 *            要查詢的 player guid
	 */
	public static void onRecvGetCommissionDetailRequest(Player player, BasicServiceModule module,
			GUID queryPlayerGuid) {
		if (player == null) {
			return;
		}

		if (module == null) {
			return;
		}

		if (queryPlayerGuid == null) {
			return;
		}

		GetCommissionDetailAsync task = new GetCommissionDetailAsync(queryPlayerGuid, player.getSessionID());
		module.addAsyncCommand(task);
	}
	
	public static boolean checkDailyExchangeChangeWay(PlayerMoneyChangeLogModel mLogModel) {
		boolean result = false;
		MoneyChangeWay changeWay = MoneyChangeWay.getChangeWay(mLogModel.getChangeWay());
		if (changeWay == null) {
			return false;
		}
		
		return changeWay.isDailyExchange();
	}
	
	public static DailyExchangeLogModel convertDailyExchange(PlayerMoneyChangeLogModel mLogModel) {
		DailyExchangeLogModel dLogModel = new DailyExchangeLogModel();
		if (checkDailyExchangeChangeWay(mLogModel) == false) {
			dLogModel = null;
		} else {
			dLogModel.setEventDate(mLogModel.getEventDate());
			dLogModel.setEventTime(mLogModel.getEventTime());
			dLogModel.setGameTime(mLogModel.getGameTime());
			dLogModel.setOrderId(mLogModel.getOrderId());
			dLogModel.setChangeWay(mLogModel.getChangeWay());
			dLogModel.setChangeWayDesc(mLogModel.getChangeWayDesc());
			dLogModel.setGoldChangeValue(mLogModel.getGoldChangeValue());
			dLogModel.setGoldAfterChange(mLogModel.getGoldAfterChange());
			dLogModel.setPlayerId(mLogModel.getPlayerId());
		}
		return dLogModel;
	}
}
